package org.example.greenhouse.service.impl;

import org.example.greenhouse.dao.DeviceDao;
import org.example.greenhouse.entity.Device;
import org.example.greenhouse.result.Result;
import org.example.greenhouse.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DeviceServiceImpl implements DeviceService {

    @Autowired
    private DeviceDao deviceDao;

    //获取所有土壤检测设备
    @Override
    public List<Device> getSoilDetectionDevices() {
        return deviceDao.findByDeviceName("土壤检测设备");
    }


    //获取所有摄像头设备
    @Override
    public List<Device> getCameraDevices() {
        return deviceDao.findByDeviceName("摄像头设备");
    }

    // 新增设备
    @Override
    public Result addDevice(Device device) {
        try {
            Device savedDevice = deviceDao.save(device);
            return Result.success(savedDevice, "设备添加成功");
        } catch (Exception e) {
            return Result.error("设备添加失败: " + e.getMessage());
        }
    }
    // 根据大棚 ID 获取设备信息
    @Override
    public Result getDevicesByGreenHouseId(Long greenHouseId) {
        try {
            List<Device> devices = deviceDao.findByGreenHouseId(greenHouseId);
            return Result.success(devices, "设备信息获取成功");
        } catch (Exception e) {
            return Result.error("获取设备信息失败: " + e.getMessage());
        }
    }

}

